package com.mk.service.platform;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.domain.TAreaMb;
import com.mk.domain.ZMembers;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.C;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 说明：会员表后台服务
 * 作者：nwl 
 * 日期：2018-10-11 14:23:59 
 * 版本：1.0.0
 */
@Controller("zMembers")
@Api(group = "pl",title = "会员表服务", path = "/zMembers")
@SuppressWarnings("unused")
public class ZMembersService extends BasePlatformService {
    @Inject
    private ZMembers.Mapper zMembersMapper;
    @Inject
    private TAreaMb.Mapper tAreaMbMapper;


    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存会员表", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "zMembers", title = "会员表", type = ZMembers.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull ZMembers zMembers) {
        String id = S2.UUID();
        zMembers.setMid(id);
        zMembers.setStatus("0");
        zMembers.setMdate(DateUtil.now());
        zMembersMapper.insertTemplate(zMembers);
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存会员表", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "zMembers", title = "会员表", type = ZMembers.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull ZMembers zMembers,String guanlianshengfen) {

        if(S.eq("1",zMembers.getStatus())){
          zMembers.setRefcode(getCode());
        }

        if(S.isNotBlank(guanlianshengfen)&&S.eq("1",zMembers.getStatus())){
            TAreaMb tAreaMb = new TAreaMb();
            tAreaMb.setMid(zMembers.getMid());
            tAreaMb.setAreacode(guanlianshengfen);
            tAreaMbMapper.insertTemplate(tAreaMb);
        }
        return zMembersMapper.updateTemplateById(zMembers);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定会员表", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "会员表的主键", type = String.class)
    @ApiReturn(description = "会员表", type = ZMembers.class)
    public ZMembers view(@NotNull String id){
        return zMembersMapper.single(id);
    }


    private String getCode() {
        String refcode = S2.randomNumberString(7);
        ZMembers zmblist = zMembersMapper.getSQLManager().selectSingle("zMembers.select_mbgetcode", C.Map("refcode", refcode), ZMembers.class);
        if (zmblist != null) {
            refcode = getCode();
        }
        return refcode;
    }
}
